package com.ruoyi.project.dict.hanyucizu.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.hanyucizu.domain.HanyuCizu;
import com.ruoyi.project.dict.hanyucizu.service.IHanyuCizuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 百度词组Controller
 *
 * @author shixiangwen
 * @date 2021-01-29
 */
@Controller
@RequestMapping("/dict/hanyucizu")
public class HanyuCizuController extends BaseController {

    private String prefix = "dict/hanyucizu";

    @Autowired
    private IHanyuCizuService hanyuCizuService;

    @RequiresPermissions("dict:hanyucizu:view")
    @GetMapping()
    public String hanyucizu() {
        return prefix + "/hanyucizu";
    }

    /**
     * 查询百度词组列表
     */
    @RequiresPermissions("dict:hanyucizu:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HanyuCizu hanyuCizu) {
        startPage();
        List<HanyuCizu> list = hanyuCizuService.selectHanyuCizuList(hanyuCizu);
        return getDataTable(list);
    }

    /**
     * 导出百度词组列表
     */
    @RequiresPermissions("dict:hanyucizu:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HanyuCizu hanyuCizu) {
        List<HanyuCizu> list = hanyuCizuService.selectHanyuCizuList(hanyuCizu);
        ExcelUtil<HanyuCizu> util = new ExcelUtil<HanyuCizu>(HanyuCizu.class);
        return util.exportExcel(list, "hanyucizu");
    }

    /**
     * 新增百度词组
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存百度词组
     */
    @RequiresPermissions("dict:hanyucizu:add")
    @Log(title = "百度词组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HanyuCizu hanyuCizu) {
        return toAjax(hanyuCizuService.insertHanyuCizu(hanyuCizu));
    }

    /**
     * 修改百度词组
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        HanyuCizu hanyuCizu = hanyuCizuService.selectHanyuCizuById(id);
        mmap.put("hanyuCizu", hanyuCizu);
        return prefix + "/edit";
    }

    /**
     * 修改保存百度词组
     */
    @RequiresPermissions("dict:hanyucizu:edit")
    @Log(title = "百度词组", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HanyuCizu hanyuCizu) {
        return toAjax(hanyuCizuService.updateHanyuCizu(hanyuCizu));
    }

    /**
     * 同步到词语库
     */
    @RequiresPermissions("dict:hanyucizu:edit")
    @Log(title = "百度词组", businessType = BusinessType.UPDATE)
    @PostMapping("/import2cizu")
    @ResponseBody
    public AjaxResult import2cizu(Long id) {
        return toAjax(hanyuCizuService.impirt2cizu(id));
    }

    /**
     * 批量同步到词语库
     */
    @RequiresPermissions("dict:hanyucizu:edit")
    @Log(title = "百度词组", businessType = BusinessType.UPDATE)
    @PostMapping("/batchimport2cizu")
    @ResponseBody
    public AjaxResult batchimport2cizu(String ids) {
        Long[] idList = Convert.toLongArray(ids);
        for (Long id : idList) {
            hanyuCizuService.impirt2cizu(id);
        }
        return toAjax(true);
    }

    /**
     * 删除百度词组
     */
    @RequiresPermissions("dict:hanyucizu:remove")
    @Log(title = "百度词组", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hanyuCizuService.deleteHanyuCizuByIds(ids));
    }
}
